﻿using System;
using System.Linq;
using eAtlantida.Tools.Generator;
using eAtlantida.Tools.Generator.Sql;

namespace eAtlantida.Tools.Operations.GenerateOperations
{
    public class GenerateStoredProcedureOperation : Operation
    {
        public const string Name = "generate sql";

        public override void Execute(State state)
        {
            var name = state.CommandParameters[0];
            var generateParameters = GetGenerateParameters(state.CommandParameters.Skip(1));

            WriteMessage("Generating stored procedures...", MessageType.Default);

            var discover = new Discover(state.ConnectionString);
            var generator = new SqlGenerator(discover.GetColumns(name), name);
            const string path = "Database/dbo/StoredProcedures";

            if (generateParameters.IsSelect)
            {
                try
                {
                    AddFile(path, name.ToLowerFirstChar() + "_select_by_id", generator.GenerateSelectByIdProcedure(), ".sql");
                    WriteMessage("Added select by Id", MessageType.Info);
                }
                catch (Exception ex)
                {
                    WriteMessage("Not added select by Id: " + ex.Message, MessageType.Error);
                }

                try
                {
                    AddFile(path, name.ToLowerFirstChar() + "_select_all", generator.GenerateSelectAllProcedure(), ".sql");
                    WriteMessage("Added select all", MessageType.Info);
                }
                catch (Exception ex)
                {
                    WriteMessage("Not added select all: " + ex.Message, MessageType.Error);
                }
            }

            if (generateParameters.IsCreate)
            {
                try
                {
                    AddFile(path, name.ToLowerFirstChar() + "_insert", generator.GenerateInsertProcedure(), ".sql");
                    WriteMessage("Added create", MessageType.Info);
                }
                catch (Exception ex)
                {
                    WriteMessage("Not added create: " + ex.Message, MessageType.Error);
                }
            }

            if (generateParameters.IsDelete)
            {
                try
                {
                    AddFile(path, name.ToLowerFirstChar() + "_delete", generator.GenerateDeleteProcedure(), ".sql");
                    WriteMessage("Added delete", MessageType.Info);
                }
                catch (Exception ex)
                {
                    WriteMessage("Not added delete: " + ex.Message, MessageType.Error);
                }
            }

            if (generateParameters.IsUpdate)
            {
                try
                {
                    AddFile(path, name.ToLowerFirstChar() + "_update", generator.GenerateUpdateProcedure(), ".sql");
                    WriteMessage("Added update", MessageType.Info);
                }
                catch (Exception ex)
                {
                    WriteMessage("Not added update: " + ex.Message, MessageType.Error);
                }
            }

            if (generateParameters.IsListing)
            {
                try
                {
                    AddFile(path, name.ToLowerFirstChar() + "_get_listing", generator.GenerateListingProcedure(), ".sql");
                    WriteMessage("Added listing", MessageType.Info);
                }
                catch (Exception ex)
                {
                    WriteMessage("Not added listing: " + ex.Message, MessageType.Error);
                }
            }
        }
    }
}